package com.vancouver.knows.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.bean.CategoryInfo;
import com.vancouver.knows.business.bean.SubcategoryInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Chris Li on 2016-07-13.
 */
public class FetchUserPostCategoriesHandler extends ACheckableJsonParser {

    public static final String USERPOST_CATEGORIES = "userpost_categories";
    public static final String CITY_LIST = "city_list";
    public static final String CITY_LIST_IMAGE = "city_list_image";

    private HashMap<String, Object> resultMap = new HashMap<String, Object>();

    @Override
    protected void parserContent(JSONObject content) throws Exception {

        String userPostCategories = content.getString(USERPOST_CATEGORIES);
        if (!StringUtil.isEmptyString(userPostCategories)) {
            List<CategoryInfo> categoryInfoList = JSON.parseArray(userPostCategories, CategoryInfo.class);
            for (CategoryInfo categoryInfo : categoryInfoList) {
                categoryInfo.getSubcategories().add(0, new SubcategoryInfo("全部", ""));
            }
            resultMap.put(USERPOST_CATEGORIES, categoryInfoList);
        } else {
            resultMap.put(USERPOST_CATEGORIES, new ArrayList<>());
        }

        String cityListString = content.getString(CITY_LIST);
        if (!StringUtil.isEmptyString(cityListString)) {
            List<String> cityList = JSON.parseArray(cityListString, String.class);
            cityList.add(0, "全部");
            resultMap.put(CITY_LIST, cityList);
        } else {
            resultMap.put(CITY_LIST, new ArrayList<>());
        }

        String cityListImageStr = content.getString(CITY_LIST_IMAGE);
        if (!StringUtil.isEmptyString(cityListImageStr)) {
            resultMap.put(CITY_LIST_IMAGE, cityListImageStr);
        } else {
            resultMap.put(CITY_LIST_IMAGE, new ArrayList<>());
        }
    }

    public HashMap<String, Object> getResultMap() {
        return resultMap;
    }

}
